package com.atguigu.controller;

import com.atguigu.common.result.R;
import com.atguigu.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Api(description = "医院接口设置")
//@CrossOrigin  //跨区域
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院设置")
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据id删除医院设置（逻辑删除）")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "医院设置主键", required = true)
                             @PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        return b ? R.ok() : R.error();
    }

    //分页查询
    @PostMapping("{page}/{limit}")
    public R pageQuery(@ApiParam(name = "page", value = "第几页", required = true)
                            @PathVariable Long page,
                       @ApiParam(name = "limit", value = "每页多少条", required = true)
                            @PathVariable Long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);

        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        //如果等于null 就是where 后面 没有条件  就是查询所有数据
        if (hospitalSetQueryVo == null) {
            hospitalSetService.page(hospitalSetPage, queryWrapper);
        } else {
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();
            if (!StringUtils.isEmpty(hoscode)) {
                queryWrapper.eq("hoscode", hoscode);
            }
            if (!StringUtils.isEmpty(hosname)) {
                queryWrapper.like("hosname", hosname);
            }
            hospitalSetService.page(hospitalSetPage, queryWrapper);
        }
        //返回值
          //获取当前页的集合
        List<HospitalSet> records = hospitalSetPage.getRecords();
        //返回页码的总条数
        long total = hospitalSetPage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation(value = "开启医院设置 ")
    @PostMapping("/saveHostSet")
    public R save(@ApiParam(name = "hospitalSet", value = "医院设置对象") @RequestBody HospitalSet hospitalSet) {
        //检查医院设置编号是否存在 -- 检查该医院是否已经开通
        String hoscode = hospitalSet.getHoscode();
        if (StringUtils.isEmpty(hoscode)) {
            return R.error().message("医院编号不能为空");
        }
        //根据 hashCode 查询 where hashCode=?
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        queryWrapper.last("OR(is_deleted=1 and hoscode=" + hoscode + ")");
        int count = hospitalSetService.count(queryWrapper);
        if (count >= 1) {
            return R.error().message("该医院已经开通了账户");
        }
        //设置医院默认状态为1
        hospitalSet.setStatus(1);
        boolean save = hospitalSetService.save(hospitalSet);
        return save ? R.ok().message("开通成功") : R.error().message("开通失败");
    }

    //根据id来查询
    @GetMapping("getHospSet/{id}")
    public R getById(@PathVariable Long id) {
        HospitalSet hospitalSetId = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSetId);

    }

    //批量删除
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<Long> idList) {
        if (idList == null || idList.size() == 0) {
            R.error().message("请传递集合");
        }
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    //根据 id来修改
    @PostMapping("updateHospSet")
    public R updateById(@RequestBody HospitalSet hospitalSet) {
        Long id = hospitalSet.getId();
        if (StringUtils.isEmpty(id)) {
            R.error().message("id不能为空");
        }
        hospitalSet.setHoscode(null);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //锁定解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        //1 参数校验 2 根据 id查询   3 重新设置状态
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)) {
            return R.error().message("状态码和id不能为空");
        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet == null) {
            return R.error().message("该医院的设置不存在");
        }
        //设置状态
        if (status != 1 && status != 0) {
            return R.error().message("参数只能为0 或者1");
        }
        hospitalSet.setStatus(status);
        hospitalSet.setUpdateTime(new Date());
        //调用修改方法
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b ? R.ok() : R.error();
    }
}
